﻿using System;
using System.Collections.Generic;
using System.Text;
using Barbar.MapReport.HtmlRender.Handlers;
using JsonSharp;
using JsonSharp.Collections;
using Barbar.MapReport.Common.Drawing.Commands;
using Barbar.MapReport.Common.Drawing;
using Barbar.MapReport.HtmlRender.Operations.Regions;
using System.Drawing;
using JsonSharp.Values;
using Barbar.MapReport.DataSource;
using Barbar.MapReport.HtmlRender.Json;
using System.Globalization;

namespace Barbar.MapReport.HtmlRender {
  internal abstract class RegionCallbackService : MapCallbackService {
    public const string FunctionName = "rptHandleRegionCallback";

    protected void AppendPolygon(JsonObjects rootObject, RegionList regionList, ZoomLevel zoomLevel, PolygonBehavior behavior, Color fillColor, Color strokeColor, int strokeWeight) {
      foreach (string regionId in regionList.RegionIds) {
        Polygon polygon = DataSourceManager.GetRegionGeometry(regionId, zoomLevel);
        Barbar.MapReport.DataSource.Region region = DataSourceManager.GetRegionById(regionId);
        string polygonKey = String.Format(CultureInfo.InvariantCulture, "s{0}_{1}", regionId, zoomLevel.Id);

        PolygonFactory factory = new PolygonFactory(polygon, fillColor, strokeColor, strokeWeight, true, behavior, regionList.ToolTip, region.Name);
        rootObject.Add(new JsonStringValue(polygonKey), factory.ToJsonObject());
      }
    }

    internal override string JavaScriptFunctionName {
      get { return FunctionName; }
    }
  }
}
